﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "UiDocumentEventHandler.h"
#include "IUiDocumentEventArgs.h"
#include "IUiDocumentViewManager.h"
#include "IDock.h"

#include "IMainWindow.h"
#include "IUiDocument.h"
#include "IDocument.h"
#include "IUiManager.h"
#include "CommandIds.h"
#include "IUiDocumentEvent.h"
#include "UiPlatformUtils.h"
#include "PropertyPalette.h"
#include "ModelViewDlgUtils.h"
#include "IRibbonContextualPage.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"


using namespace Sample;
using namespace gcmp;

UiDocumentEventHandler::UiDocumentEventHandler()
{
    IUiDocumentViewManager *pUiDocumentViewManager = UiPlatformUtils::GetUiDocumentViewManager();
    if (pUiDocumentViewManager)
    {
        IUiDocumentEvent *event = pUiDocumentViewManager->GetUiDocumentEvent();
        if (event)
        {
            event->Add(this);
        }
    }
}

UiDocumentEventHandler::~UiDocumentEventHandler()
{
    IUiDocumentViewManager *pUiDocumentViewManager = UiPlatformUtils::GetUiDocumentViewManager();
    if (pUiDocumentViewManager)
    {
        IUiDocumentEvent *event = pUiDocumentViewManager->GetUiDocumentEvent();
        if (event)
        {
            event->Delete(this);
        }
    }
}

void UiDocumentEventHandler::CheckDockerStatus(IUiDocument* pUiDocument)
{
    IDockManager* pDockManager = IUiManager::Get()->GetDockManager();

    PropertyPalette::DisplayPanel();//显示属性面板
                                    
    // 下面代码需要在UIView初始化之前，从而可以挂到UIView的事务
    ModelViewDlgUtils::DisplayPanel(pUiDocument); // 资源管理器
}

void UiDocumentEventHandler::On(IUiDocumentEventArgs* pArgs)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pArgs,L"Ui文档事件参数不能为空",L"GDMP",L"2023-09-30");
    gcmp::IUiDocument* pUiDocument = pArgs->GetUiDocument();
    UiDocumentEventType uiDocumentEventType = pArgs->GetType();
    switch (pArgs->GetType())
    {
    case UiDocumentEventType::PreUiDocumentActivate:
    {
        IMainWindow::GetMainWindow()->ShowMaximized();
        break;
    }
    case UiDocumentEventType::UiDocumentActivated:
    {
        CheckDockerStatus(pArgs->GetUiDocument());
        RegisterElementsChangeEvent(pUiDocument->GetDbDocument());
        break;
    }
    case UiDocumentEventType::PreUiDocumentClose:
    {
        UnregisterElementsChangeEvent(pUiDocument->GetDbDocument());
        break;
    }
    default:
        break;
    }
}

void UiDocumentEventHandler::On(gcmp::IElementsChangeEventArgs* pArgs)
{
    if (nullptr == pArgs)
    {
        return;
    }
}

bool UiDocumentEventHandler::RegisterElementsChangeEvent(gcmp::IDocument* pDocument)
{
    if (nullptr == pDocument)
    {
        return false;
    }
    UnregisterElementsChangeEvent(pDocument);

    bool result = pDocument->AddElementsChangeEventHandler(ElementsChangeEventType::ElementsAdditionEvent, this);

    return result;
}

bool UiDocumentEventHandler::UnregisterElementsChangeEvent(gcmp::IDocument* pDocument)
{
    if (nullptr == pDocument)
    {
        return false;
    }
    bool result = pDocument->DeleteElementsChangeEventHandler(ElementsChangeEventType::ElementsAdditionEvent, this);

    return result;
}
